﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pe.Petecos.Comercial.Aplicacion.Core.Base
{
    public class Formato
    {
        public static DateTime? StringFormatDatetime(string value, string format = "dd/MM/yyyy")
        {

            return string.IsNullOrEmpty(value) ? (DateTime?)null : DateTime.ParseExact(value, format, System.Globalization.CultureInfo.InvariantCulture);

        }

        public static decimal? StringFormatDecimal(string value, string format = "{0:#,##0.00}")
        {

            if (!format.Contains("{"))
                format = "{0:" + format + "}";

            NumberFormatInfo numberFormatInfo = new NumberFormatInfo();
            //numberFormatInfo.NumberDecimalSeparator = numberFormat.NumberDecimalSeparator;
            //numberFormatInfo.NumberGroupSeparator = numberFormat.NumberGroupSeparator;

            return string.IsNullOrEmpty(value) ? (decimal?)null : Decimal.Parse(value, numberFormatInfo);

        }


        /// <summary>
        /// Permite convertir el formato DateTime
        /// </summary>
        /// <param name="value">valor</param>
        /// <param name="format">Formato</param>
        /// <returns>Retorna formato DateTime</returns>
        public static string DatetimeFormatString(DateTime? value, string format = "dd/MM/yyyy")
        {
            if (value.HasValue)
                return Convert.ToDateTime(value).ToString(format, System.Globalization.CultureInfo.InvariantCulture);
            else
                return string.Empty;
        }

        /// <summary>
        /// Permite Formato cadena decimal
        /// </summary>
        /// <param name="value">valor</param>
        /// <param name="format">formato</param>
        /// <param name="numberFormat">número formato</param>
        /// <returns>Cadena de Decimal Formateada</returns>
        public static string DecimalFormatString(decimal? value, string format = "{0:#,##0.00}")
        {
            if (value.HasValue)
            {
                if (!format.Contains("{"))
                    format = "{0:" + format + "}";

                NumberFormatInfo numberFormatInfo = new NumberFormatInfo();
                //numberFormatInfo.NumberDecimalSeparator = numberFormat.NumberDecimalSeparator;
                //numberFormatInfo.NumberGroupSeparator = numberFormat.NumberGroupSeparator;

                //format = format.Replace(numberFormat.NumberDecimalSeparator, "@");
                //format = format.Replace(numberFormat.NumberGroupSeparator, ",");
                //format = format.Replace("@", ".");

                return string.Format(numberFormatInfo, format, value);
            }
            else
                return string.Empty;
        }
    }
}
